<template>
	<view class="page">
		<view style="display: flex;flex-direction: row;justify-content: space-between;background: #FFFFFF;">
			<view style="margin-left: 176upx;display: flex;flex-direction: column;align-items: center;"
				@click="switchTab(1)">
				<view style="font-size: 32upx;color: #191919;font-weight: bold;">
					需求
				</view>
				<image v-if="isSelect" style="width: 48upx;height: 16upx;" src="../../static/images/publish-scoll.png"
					mode=""></image>
			</view>
			<view style="margin-right: 176upx;display: flex;flex-direction: column;align-items: center;"
				@click="switchTab(2)">
				<view style="font-size: 32upx;color: #191919;font-weight: bold;">
					源码
				</view>
				<image v-if="!isSelect" style="width: 48upx;height: 16upx;" src="../../static/images/publish-scoll.png"
					mode=""></image>
			</view>


		</view>

		<view style="margin-top: 24upx;width: 702upx;height: 354upx;background: #FFFFFF;
		border-radius: 20upx;margin-left: 24upx;margin-right: 24upx;" v-for="(item,index) in demandList" :key="index"
			v-if="isSelect">
			<view style="display: flex;flex-direction: row;justify-content: space-between;">
				<view style="display: flex;flex-direction: row;align-items: center;">
					<image style="width: 40upx;height: 40upx;margin-top: 24upx;margin-left: 24upx;"
						src="../../static/images/demand-order-img.png" mode=""></image>
					<view
						style="color: #202021;font-weight: bold;font-size: 30upx;margin-left: 16upx;margin-top: 24upx;">
						{{item.demandNo}}
					</view>
				</view>
				<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;margin-right: 24upx;"
					v-if="item.status===1">待审核</view>
				<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;margin-right: 24upx;"
					v-if="item.status===2">审核未通过</view>
				<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;margin-right: 24upx;"
					v-if="item.status===3">待接单</view>
				<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;margin-right: 24upx;"
					v-if="item.status===4">已接单</view>
				<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;margin-right: 24upx;"
					v-if="item.status===5">进行中</view>
				<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;margin-right: 24upx;"
					v-if="item.status===6">已完成</view>

			</view>

			<view style="display: flex;flex-direction: row;align-items: center;" @click="toDemandDetail(item.id)">
				<image style="width: 140upx;height: 140upx;border-radius: 16upx;margin-top: 24upx;margin-left: 24upx;"
					:src="item.image" mode="aspectFill" v-if="item.image!==null"></image>
				<view style="display: flex;flex-direction: column;margin-left: 24upx;margin-top: 24upx;"
					v-if="item.image!==null">
					<view style="font-size: 28upx;color: #191919;">{{item.title}}</view>
					<view style="color: #FF4E4C;font-size: 24upx;font-weight: bold;margin-top: 4upx;">
						￥{{item.estimateAmount}}</view>
					<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;"> {{item.publishTime}}</view>
				</view>
				<view style="display: flex;flex-direction: column;margin-left: 80upx;margin-top: 24upx;"
					v-if="item.image===null">
					<view style="font-size: 28upx;color: #191919;">{{item.title}}</view>
					<view style="color: #FF4E4C;font-size: 24upx;font-weight: bold;margin-top: 4upx;">
						￥{{item.estimateAmount}}</view>
					<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;"> {{item.publishTime}}</view>
				</view>
			</view>



			<view style="display: flex;flex-direction: row-reverse;margin-top: 40upx;">
				<view style="width: 60upx;height: 60upx;border: 2upx solid #EDEDED;display: flex;justify-content: center;align-items: center;
				border-radius: 30upx;text-align: center;line-height: 60upx;margin-right: 24upx;">

					<image style="width: 40upx;height: 40upx;" src="../../static/images/edit-icon.png" mode=""
						@click="clickEdit(1,item.id)"></image>
				</view>
				<!-- <view style="border-radius: 30upx;height: 60upx;width: 104upx;border: 2upx solid #EDEDED;
				color: #191919;font-size: 24upx;text-align: center;line-height: 60upx;margin-right: 16upx;">编辑</view> -->
			</view>


		</view>



		<view style="margin-top: 24upx;width: 702upx;height: 296upx;background: #FFFFFF;
		border-radius: 20upx;margin-left: 24upx;margin-right: 24upx;" v-for="(item,index) in sourceCodeList" :key="index"
			v-if="!isSelect">

			<view style="display: flex;flex-direction: row;align-items: center;" @click="toCodeDetail(item.id)">
				<image style="width: 140upx;height: 140upx;border-radius: 16upx;margin-top: 32upx;margin-left: 24upx;"
					:src="item.images" mode="aspectFill"></image>
				<view style="display: flex;flex-direction: column;margin-left: 24upx;margin-top: 32upx;">
					<view style="font-size: 28upx;color: #191919;">{{item.title}}</view>
					<view style="color: #FF4E4C;font-size: 32upx;font-weight: bold;margin-top: 4upx;">￥{{item.price}}
					</view>
					<view style="color: #9E9E9E;font-size: 24upx;margin-top: 24upx;">{{item.createTime}}发布</view>
				</view>
			</view>

			<view style="display: flex;flex-direction: row-reverse;margin-top: 40upx;">
				<view style="width: 60upx;height: 60upx;border: 2upx solid #EDEDED;display: flex;justify-content: center;align-items: center;
				border-radius: 30upx;text-align: center;line-height: 60upx;margin-right: 24upx;">
					<image style="width: 40upx;height: 40upx;" src="../../static/images/edit-icon.png" mode=""
						@click="clickEdit(2,item.id)"></image>
				</view>
				<!-- <view style="border-radius: 30upx;height: 60upx;width: 104upx;border: 2upx solid #EDEDED;
				color: #191919;font-size: 24upx;text-align: center;line-height: 60upx;margin-right: 16upx;">编辑</view> -->
			</view>



		</view>

		<view style="display: flex;flex-direction: column;margin-top: 310upx;align-items: center;background: #F8F8F8;"
			v-if="isShow">
			<image style="width: 350upx;height: 240upx;" src="../../static/images/publish-no-icon.png" mode=""></image>
			<view style="color: #B7B8BB;font-size: 28upx;margin-top: 20upx;">没有相关发布~</view>
			<view
				style="width: 292upx;height: 72upx;color: #FFFFFF;background: #FF4E4C;line-height: 72upx;text-align: center;border-radius: 44upx;margin-top: 64upx;"
				@click="toPublish">立即发布</view>
		</view>
		
		<view class="popup" v-show="show">
			<view class="popup-info">
				<view style="display: flex;flex-direction: column;align-items: center;">
					<view style="color: #191919;font-size: 32upx;margin-top: 48upx;">是否确认删除?</view>
				</view>
				
				<view style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;margin-top: 56upx;">
					<view style="background: #F4F4F4;color: #191919;font-size: 28upx;width: 240upx;height: 80upx;margin-left: 30upx;border-radius: 32upx;text-align: center;line-height: 80upx;" @tap="cancel">取消</view>
					<view style="background: #FF4E4C;color: #FFFFFF;font-size: 28upx;width: 240upx;
					height: 80upx;margin-right: 30upx;border-radius: 32upx;text-align: center;line-height: 80upx;" @tap="confrim">确认</view>
				</view>
			</view>
		</view>

	</view>
</template>


<script>
	import {
		demand_list,
		delete_demand
	} from '../../api/mine/mine-demand.js'
	import {
		source_code_list,
		delete_code
	} from '../../api/mine/mine-source-code.js'
	import {
		source_code_detail
	} from '../../api/source-code/source-code.js'
	export default {
		data() {
			return {
				isSelect: false,
				demandList: [],
				isShow: false,
				sourceCodeList: [],
				demand: {},
				show:false,
				switchCode:1,
				demandImage: "",
				type:"",
				detailDemandId:"",
				detailCodeId:""
			}
		},
		onLoad(options) {
			let code = options.code
			if (code == 1) {
				this.isSelect = true
				this.switchCode=1
				this.getDemandList()
			} else {
				this.switchCode=2
				this.getSourceCodeList();
			}
		},
		methods: {
			popConfirm() {
				this.show = true
			},
			// 点击弹窗取消
			cancel() {
				this.show = false;
			},
			// 点击弹窗确认
			confrim() {
				if(this.type===1){
					let that = this
					this.show=false
					let params = {}
					params.demandId = this.detailDemandId
					delete_demand(params).then(res => {
						if (res.status === 200) {
							setTimeout(function(){
								uni.showToast({
									icon:'none',
									title:'删除成功',
									duration:2000
								})
							},500)
							that.getDemandList()
						}
					})
				}else{
					let that = this
					this.show=false
					let params = {}
					params.sourceCodeId = this.detailCodeId
					delete_code(params).then(res => {
						if (res.status === 200) {
							setTimeout(function(){
								uni.showToast({
									icon:'none',
									title:'删除成功',
									duration:2000
								})
							},500)
							that.getSourceCodeList()
						}
					})
				}
				
				
				
			},
			clickEdit(type, id) {
				let that = this
				//需求
				if (type === 1) {
					this.type=1
					uni.showActionSheet({
						itemList: ['删除'],
						success(res) {
							let code = res.tapIndex + 1
							if (code === 1) {
								//删除
								that.detailDemandId=id
								that.show=true
							}
						}
					})
				} else {
					this.type=2
					uni.showActionSheet({
						itemList: ['删除'],
						success: (res) => {
							//源码
							that.detailCodeId=id
							that.show=true
						}
					})

				}


			},


			toCodeDetail(id) {
				uni.navigateTo({
					url: '../code-detail/code-detail?id=' + id
				})
			},
			switchTab(code) {
				this.isSelect = !this.isSelect
				console.log(code)
				if (code == 1) {
					this.switchCode=1
					this.getDemandList();
				} else {
					this.switchCode=2
					this.getSourceCodeList();
				}
			},

			toPublish() {
				if (this.switchCode == 1) {
					uni.navigateTo({
						url: '../publish-demand/publish-demand'
					})
				} else {
					uni.navigateTo({
						url: '../publish-code/publish-code'
					})
				}
			},
			getDemandList() {
				let params = {}
				this.isShow = false;
				demand_list(params).then(res => {
					if (res.status == 200) {
						if (res.data !== null) {
							this.demandList = res.data
						} else {
							this.demandList=[]
							this.isShow = true;
						}
					}
				})

			},
			getSourceCodeList() {
				let params = {}
				this.isShow = false;
				source_code_list(params).then(res => {

					if (res.status == 200) {
						if (res.data !== null) {

							this.sourceCodeList = res.data
						} else {
							this.sourceCodeList=[]
							this.isShow = true;
						}
					}
				})
			},
			toDemandDetail(id) {
				uni.navigateTo({
					url: '../demand-detail/demand-detail?id=' + id
				})
			}
		}
	}
</script>

<style>
	button::after {
		border: none;
	}
	
	.popup-info {
		position: fixed;
		width: 600upx;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		padding: 40upx;
		border-radius: 20upx;
		background-color: #fff;
		z-index: 9999;
	}
	
	.popup {
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		height: 100vh;
		background-color: rgba(0, 0, 0, 0.6);
		z-index: 9998;
	}
</style>
